//
//  ConsultViewController.m
//  drug
//
//  Created by 沈鑫 on 16/4/5.
//  Copyright © 2016年 沈鑫. All rights reserved.
//

#import "ConsultViewController.h"
#import "ConsultCollectionViewCell.h"
#import "RequestManager.h"
#import "ConsultModel.h"
@interface ConsultViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,UICollectionViewDelegateFlowLayout>



@property(nonatomic,strong)UIView *miniView;

@property(nonatomic,strong)UIView *hengView;



@property(nonatomic,strong)UIButton *yongButton;


@property(nonatomic,strong)UIButton *shouButton;



@property(nonatomic,strong)UIScrollView *scrollView;


@property(nonatomic,strong)UIView *myView;


@property(nonatomic,strong)UICollectionView *collectionView;


@property(nonatomic,strong)NSMutableArray *array;


@end

@implementation ConsultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, KWIDTH, KHEIGHT - 114)];
    _scrollView.contentSize = CGSizeMake(KWIDTH * 2, 0);
    _scrollView.contentOffset = CGPointMake(0, 0);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_scrollView];
    
    
    
    UICollectionViewLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, KWIDTH, KHEIGHT - 114 - 44) collectionViewLayout:flowLayout];
    
    // 设置代理
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor lightGrayColor];
    // 注册cell
    [_collectionView registerClass:[ConsultCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.scrollView addSubview:_collectionView];
    
    
    
    
    

    _miniView = [[UIView alloc]initWithFrame:CGRectMake(KWIDTH / 2, 0, 1, 40)];
    _miniView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_miniView];
    
    
    _hengView = [[UIView alloc]initWithFrame:CGRectMake(0, 40, KWIDTH, 1)];
    _hengView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_hengView];
    
    
    _yongButton  = [[UIButton alloc]initWithFrame:CGRectMake(40, 7, 100, 30)];
    [_yongButton setTitle:@"用药收藏" forState:UIControlStateNormal];
    [_yongButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    _yongButton.tag = 100;
    [_yongButton addTarget:self action:@selector(yongButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_yongButton];
    
    
    _myView = [[UIView alloc]initWithFrame:CGRectMake(55, 38, 70, 2)];
    _myView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_myView];
    
    
    
    _shouButton = [[UIButton alloc]initWithFrame:CGRectMake(230, 7, 100, 30)];
    [_shouButton setTitle:@"收藏咨询" forState:UIControlStateNormal];
    [_shouButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _shouButton.tag = 200;
    [_shouButton addTarget:self action:@selector(shouButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_shouButton];
    
    

    [self request];
    
}




#pragma mark --- 网络请求 ---
-(void)request
{
    
    
    [RequestManager requestWithUrl:@"http://apphy02.caeac.com.cn/500e32BD52027E652AA00003/androidapi/indexarticlec.php?w=1284&&n=20&p=0&t=36" requestType:RequestTypeGET parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        self.array = [ConsultModel getData:dic];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.collectionView reloadData];
        
        });
        
        
        
    } error:^(NSError *error) {
        NSLog(@"网络错误");
    }];
    
    
    
}





#pragma mark --- 返回行数 ---
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
   
    return _array.count;
    
}

#pragma mark --- 返回cell ---
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ConsultCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ConsultModel *model = _array[indexPath.row];
    
    
    [cell getData:model];
    
    
    return cell;
    
}


#pragma mark --- 返回每个的高度和宽度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((KWIDTH  - 20)/ 2,(KWIDTH  - 20)/ 2);
}



#pragma mark --- 返回集合视图整体上 左 下 右编剧
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(1, 1, 1, 1);
}



#pragma mark --- 控制集合视图的行边距 ---
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}




#pragma mark --- 控制集合视图的列边距 ---
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}



#pragma mark --- cell点击方法 ---
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    
    
    
    
}








#pragma mark --- 结束减速会触发的方法 ---
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (_scrollView.contentOffset.x == 0)
    {
        [_yongButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_shouButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:0.5 animations:^{
            
            _myView.frame = CGRectMake(55, 38, 70, 2);
            
        }];
        
        
        
        
        
        
    }else
    {
        [_shouButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_yongButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:0.5 animations:^{
            
            _myView.frame = CGRectMake(245, 38, 70, 2);
            
        }];
        
        
    }
    
    
    
    
    
}


-(void)yongButtonAction:(UIButton *)button
{
    [_yongButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [_shouButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [UIView animateWithDuration:0.5 animations:^{
        _scrollView.contentOffset = CGPointMake(0, 0);
    }];
    
    [UIView animateWithDuration:0.5 animations:^{
        
        _myView.frame = CGRectMake(55, 38, 70, 2);
        
    }];
    
    
    
    
}


-(void)shouButtonAction:(UIButton *)button
{
    [_yongButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [_shouButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    
    
    [UIView animateWithDuration:0.5 animations:^{
        _scrollView.contentOffset = CGPointMake(KWIDTH, 0);
        
    }];
    
    
    [UIView animateWithDuration:0.5 animations:^{
        
        _myView.frame = CGRectMake(245, 38, 70, 2);
        
    }];
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
